import { getSearch, getOne, getUpdate, getHistory, deleteOne, getCash, getAfterOrder ,getAfterOrderOne , updateAfterOrder } from '@/apis/order'
import { ProductModelType } from '../interface/orderInterface'
import { Button, message, Space } from 'antd';

const orderModel: ProductModelType = {
    namespace: "order",
    state: {
        order: [],
        updateOrder: [],
        updatedate: [],
        history: [],
        deleteDate: [],
        casDate: [],
        saleDate: [],
        saleGood:{}
    },
    effects: {
        //query就是一个函数，函数的类型Effect
        *query({ payload }, { call, put }): any {
            const res: any = yield call(getSearch, payload)
            // console.log("payload",payload);
            yield put({
                type: "initData",
                payload: res.data.data
            });

        },

        *queryHistory({ payload }, { call, put }): any {
            const res: any = yield call(getHistory, payload)
            // console.log("payload", res.data.data);
            yield put({
                type: "historyDate",
                payload: res.data.data
            });

        },

        *updateGood({ payload }, { call, put }): any {
            const res: any = yield call(getOne, payload)
            if (res.data.meta.status == '200') {
                yield put({
                    type: "updateInitData",
                    payload: res.data.data
                });
            }
        },

        *update({ payload }, { call, put }): any {
            const res: any = yield call(getUpdate, payload)
            // console.log("payload",payload);
            // console.log("res",res.data);
            yield put({
                type: "updateDate",
                payload: res.data.data
            });
        },
        *deleteManage({ payload }, { call, put }): any {
            const res: any = yield call(deleteOne, payload)
            // console.log("payload",payload);
            // console.log("delete", res.data);
            if (res.data.meta.status == '201') {
                message.open({
                    type: 'error',
                    content: res.data.meta.msg,
                });
            }
            yield put({
                type: "deleteManageDate",
                payload: res.data.data
            });
        },
        *queryCas({ payload }, { call, put }): any {
            const res: any = yield call(getCash, {...payload})
            // console.log("getCash", payload);
            // console.log("getCash", res.data.data);
            if (res.data.meta.status == '200') {
                yield put({
                    type: "initCasData",
                    payload: res.data.data
                });
            }
        },

        *querySale({ payload }, { call, put }): any {
            const res: any = yield call(getAfterOrder, payload)
            // console.log("getAfterOrder", res.data);
            if (res.data.meta.status == '200') {
                yield put({
                    type: "initSaleData",
                    payload: res.data.data
                });
            }
        },

        *saleGood({ payload }, { call, put }): any {
            const res: any = yield call(getAfterOrderOne, payload)
            //  console.log("getAfterOrderOne", res.data);
            if (res.data.meta.status == '200') {
                yield put({
                    type: "saleGoodData",
                    payload: res.data.data
                });
            }
        },

        *afterNode({ payload }, { call, put }): any {
            const res: any = yield call(updateAfterOrder, payload)
            //  console.log("updateAfterOrder", res.data);
            // if (res.data.meta.status == '200') {
            //     yield put({
            //         type: "afterNodeData",
            //         payload: res.data.data
            //     });
            // }
        },

    },
    reducers: {
        initData(state, action) {
            // console.log(action.payload);
            state.order = action.payload
        },
        updateInitData(state, action) {
            state.updateOrder = action.payload
        },
        updateDate(state, action) {
            state.updatedate = action.payload
        },
        historyDate(state, action) {
            state.history = action.payload
        },
        deleteManageDate(state, action) {
            state.deleteDate = action.payload
        },
        initCasData(state, action) {
            state.casDate = action.payload
        },
        initSaleData(state, action) {
            state.saleDate = action.payload
        },
        saleGoodData(state, action) {
            state.saleGood = action.payload
        },
        afterNodeData(state, action) {
            // state.saleGood = action.payload
        },
        // subscriptions: { setup: Subscription };
    }
}

export default orderModel